const QRCode = require("./QRCode")
function buildQRCode(id, content, config) {
  new QRCode(id, {
    text: content,
    width: config?.w || 200,
    height: config?.h || 200,
    padding: 12, // 生成二维码四周自动留边宽度，不传入默认为0
    correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
    callback: config?.fn || ((res) => { })
  })
}

function selected(dataList, superClass, subClass, _this) {
  const indey = dataList.findIndex(item => item.superclass == superClass)
  if (indey != -1) {
    let index = dataList[indey].subclass.indexOf(subClass);
    if (index !== -1) {
      dataList[indey].subclass.splice(index, 1); // 删除该元素
      _this.setData({
        selectedData: dataList
      })
    } else {
      dataList[indey].subclass.push(subClass)
      _this.setData({
        selectedData: dataList
      })
    }
  } else {
    const temp = {
      superclass: superClass,
      subclass: [subClass]
    }
    dataList.push(temp)
    _this.setData({
      selectedData: dataList
    })
  }
}
function exist(dataList, superClass, subClass) {
  const index = dataList.findIndex(item => item.superclass == superClass)
  if (index != -1) {
    return dataList[index].subclass.includes(subClass)
  } else {
    return false
  }
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  exist,
  selected,
  buildQRCode
}
